﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;

namespace WebApplication.Admin
{
    public partial class XuatPhieuThu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadNhomhang();
              
            }
        }
        public void LoadNhomhang()
        {
            IUnitOfWork iunitwork = new EFUnitOfWork();
            NHOMHANGRepository repository = new NHOMHANGRepository(new EFRepository<NHOMHANG>(), iunitwork);
            IQueryable<NHOMHANG> objnhomhang = repository.All();
            Dlnhomhang.DataSource = objnhomhang;
            Dlnhomhang.DataValueField = "MANH";
            Dlnhomhang.DataTextField = "TENNHOMHANG";
            Dlnhomhang.DataBind();
            Dlnhomhang.Items.Insert(0, new ListItem { Text = "---Chọn nhóm hàng---", Value = "0" });
        }
        public void LoadKhachHangTheoIDnhomhang(string manh)
        {
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            KHACHHANGRepository repository = new KHACHHANGRepository(new EFRepository<KHACHHANG>(), iunitfwork);
            IQueryable<KHACHHANG> objkhachhang = repository.All().Where(p => p.MANH == manh);
            comboboxs.DataSource = objkhachhang;
            comboboxs.DataValueField = "MAKH";
            comboboxs.DataTextField = "TENKH";
            comboboxs.DataBind();
            comboboxs.Items.Insert(0, new ListItem { Text = "---Chọn khách hàng---", Value = "0" });

        }
        protected void Dlnhomhang_SelectedIndexChanged(object sender, EventArgs e)
        {
            string manh = Dlnhomhang.SelectedValue;
            LoadKhachHangTheoIDnhomhang(manh);
            if (Dlnhomhang.SelectedValue.Equals("0"))
            {
                Dlnhomhang.SelectedValue = "0";
            }

        }

        protected void ltbnxem_Click(object sender, EventArgs e)
        {
            string makh = comboboxs.SelectedValue;
            string ngay = DateTime.Now.ToString("dd/MM/yyyy");
            var source = new Microsoft.Reporting.WebForms.ReportDataSource("XuatPhieuThu", new Baocao.XuatPhieuThu().GetXuatPhieuThu(makh, ngay));
            ReportViewer1.LocalReport.ReportPath = @"Admin\Baocao\XuatPhieuThu.rdlc";
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(source);
            ReportViewer1.LocalReport.Refresh();
        }
    }
}